package edu.fh.salzburg.its.opsystems.newfs.tester;

import java.io.File;
import java.util.EnumSet;
import java.util.List;
import java.util.Random;

import edu.fh.salzburg.its.opsystems.newfs.definition.FileAttributes;
import edu.fh.salzburg.its.opsystems.newfs.definition.NewFS;
import edu.fh.salzburg.its.opsystems.newfs.definition.NewFSElement;
import edu.fh.salzburg.its.opsystems.newfs.exception.NewFSException;
import edu.fh.salzburg.its.opsystems.newfs.implementation.MBRADirectory;
import edu.fh.salzburg.its.opsystems.newfs.implementation.MBRAFile;
import edu.fh.salzburg.its.opsystems.newfs.implementation.MBRAFileSystem;

public class NewFSTester {

	public static void main(String[] args) throws Exception {
		NewFS fs = new MBRAFileSystem();
		
		testFS(fs);
		File f = new File("testFS.bin");
		fs.writeFsToRealFile(f);
		NewFS fs2 = new MBRAFileSystem();
		fs2.initializeFromFile(f);
		test2FS(fs2);
	}
	
	private static Byte[] createRandomContent (int size) {
		byte[] randomBytes =  new byte[size];
		Byte[] bigRandomBytes =  new Byte[size];
		Random rand = new Random();
		rand.nextBytes(randomBytes);
		for(int i = 0; i < randomBytes.length; i++) {
			bigRandomBytes[i] = new Byte(randomBytes[i]);
		}
		return bigRandomBytes;
	}
	
	private static boolean equalsByteArray(Byte[] actual, Byte[] expected) {
		for (int i = 0; i < expected.length; i++) {
			if (!actual[i].equals(expected[i])) {
				return false;
			}
		}
		return true;
	}
	
	private static void testFS(NewFS fs) throws NewFSException {
		System.out.println("");
		System.out.println("------------------Writing Random File ");
		MBRAFile newFile = new MBRAFile("file1.txt");
		newFile.setIdentifer("1");
		newFile.setAttributes(EnumSet.of(FileAttributes.ARCHIV));
		Byte[] newContent = createRandomContent(300); 
		fs.write(newFile, newContent);

		System.out.println("");
		System.out.println("------------------Create Directory dir1");
		MBRADirectory dir = new MBRADirectory("dir1");
		fs.createDirectory(dir);
		
		System.out.println("");
		System.out.println("------------------Writing Random File 2");
		MBRAFile newFile2 = new MBRAFile("file2.txt");
		newFile2.setIdentifer("2");
		newFile2.setPath("dir1");
		Byte[] newContent2 = createRandomContent(600); 
		fs.write(newFile2, newContent2);

		System.out.println("");
		System.out.println("------------------get Attributes file1.txt ");
		List<String> cont_s = fs.getAttributes(newFile);
		for (String el : cont_s) {
			System.out.println(el);
		}

		System.out.println("");
		System.out.println("------------------get Attributes dir1");
		cont_s = fs.getAttributes(dir);
		for (String el : cont_s) {
			System.out.println(el);
		}

		System.out.println("");
		System.out.println("------------------Reading Random File ");
		Byte[] contentInFS = fs.read(newFile);
		System.out.println("Content is ok: " + equalsByteArray(contentInFS, newContent));

		System.out.println("");
		System.out.println("------------------Reading Random File 2");
		contentInFS = fs.read(newFile2);
		System.out.println("Content is ok: " + equalsByteArray(contentInFS, newContent2));

		System.out.println("");
		System.out.println("------------------Checking status: ");
		cont_s = fs.checkFilesystem();
		for (String string : cont_s) {
			System.out.println("Status info: " + string);
		}
		
		System.out.println("");
		System.out.println("------------------List root: ");
		MBRADirectory root = new MBRADirectory("root");
		List<NewFSElement> cont = fs.getDirectoryContents(root);
		for (NewFSElement el : cont) {
			System.out.println(el.toString());
		}

		System.out.println("");
		System.out.println("------------------List dir1: ");
		cont = fs.getDirectoryContents(dir);
		for (NewFSElement el : cont) {
			System.out.println(el.toString());
		}

		System.out.println("");
		System.out.println("------------------del file2.txt:");
		fs.deleteFile(newFile2);

		System.out.println("");
		System.out.println("------------------List dir1: ");
		cont = fs.getDirectoryContents(dir);
		for (NewFSElement el : cont) {
			System.out.println(el.toString());
		}

		System.out.println("");
		System.out.println("------------------del dir1: ");
		fs.deleteDirectory(dir);

		System.out.println("");
		System.out.println("------------------Checking status after del dir1: ");
		cont_s = fs.checkFilesystem();
		for (String string : cont_s) {
			System.out.println("Status info: " + string);
		}
	}

	private static void test2FS(NewFS fs) throws NewFSException {
		System.out.println("");
		System.out.println("------------------Checking status: ");
		List<String>cont_s = fs.checkFilesystem();
		for (String string : cont_s) {
			System.out.println("Status info: " + string);
		}
		
		System.out.println("");
		System.out.println("------------------List root: ");
		MBRADirectory root = new MBRADirectory("root");
		List<NewFSElement> cont = fs.getDirectoryContents(root);
		for (NewFSElement el : cont) {
			System.out.println(el.toString());
		}
	}
}